#!/bin/bash

#. /etc/rc.d/init.d/functions

action()
{
	code=$1
	message=$2
	$MOVE_TO_COL
	if [[ "0" == "$code" ]]; then
		if [ "" == "$message" ]; then
			message="OK"
		fi
		echo -e " [\033[32m $message \033[0m]"
		echo -ne "\r"
	else
		if [ "" == "$message" ]; then
			message="FAILD"
		fi
		echo -e " [\033[31m $message \033[0m]"
		echo -ne "\r"
	fi
	return $code
}

wwwroot=$(cd `dirname $0`;pwd)
chmod a+x "$wwwroot/think"
command="$wwwroot/think queue:work --daemon"


result=$(ps -ef | grep -v "grep" | grep "`echo $command`")

if [ ! -n "$result" ]
then
	echo -n "Starting the process."
	string=$(nohup $command >/dev/null 2>&1 &)
	action 0
else
	echo "The process has been started."
fi

result=$(crontab -l | grep -v "grep" | grep -i "* * * * * $wwwroot/queue.sh")

if [ ! -n "$result" ]
then
	echo "Creating queue crontab..."
	crontab -l > /tmp/crontab
	echo -e "* * * * * $wwwroot/queue.sh\n" >> /tmp/crontab
	crontab /tmp/crontab
	rm -f /tmp/crontab
	echo -n "Creating queue crontab success."
	action 0
else
	echo -n "The queue crontab has been added."
	action 0
fi

